package com.itheima.web.base;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;


public class BaseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //0.设置请求编码 设置编码, 一定要放在获取参数之前才可以!!!
            request.setCharacterEncoding("UTF-8");

            //1. 获取method参数
            String methodName = request.getParameter("method");
            //2. 判断-反射实现
            //2.1 获取当前类的Class对象
            Class clazz = this.getClass();
            //2.2 获取方法对象
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //2.3 调用类的方法
            method.invoke(this,request,response );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }


}
